//
//  NetWorkTools.swift
//  sinaweibowj
//
//  Created by Macintosh on 15/12/21.
//  Copyright © 2015年 mactoinsh. All rights reserved.
//

import AFNetworking
let dataErrorDomain = "com.baidu.data.error"

//任意枚举类型
enum HTTPMethod :String{
    case GET = "GET"
    case POST = "POST"
}

//网络请求的处理层 以后的网络请求都使用这个类来进行获取
class NetWorkTools: AFHTTPSessionManager {
    //单例对象
    static let sharedTools:NetWorkTools = {
        //设置baseurl
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        let tools = NetWorkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    //网络请求的核心方法封装
    func requestJSONDict(method:HTTPMethod, urlString:String,parameters:[String:String],finished:(dict:[String:AnyObject]?,error:NSError?) -> ()){
        
        if method == HTTPMethod.POST {
            //发送POST请求
            POST(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result ) -> Void in
                    //请求成功的回调
                    print(result)
                    
                    guard let dict = result as? [String:AnyObject]else {
                        //不能转换为字典数据，执行失败的回调
                        //domain反转的域名 code 错误状态码，一般使用负数
                        let myError = NSError(domain: dataErrorDomain, code: -100000, userInfo: [ NSLocalizedDescriptionKey:"数据不合法"])
                        finished(dict: nil , error: myError)
                        return
                    }
                    //执行成功的回调
                    finished(dict: dict, error: nil)
                    
                }) { (_, error) -> Void in
                    //请求失败的回调
                    print(error)
                    //执行失败的回调
                    finished(dict: nil , error: error)
            }

        } else {
            //发送GET请求
            GET(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result ) -> Void in
                    //请求成功的回调
                    //print(result)
                    
                    guard let dict = result as? [String:AnyObject]else {
                        //不能转换为字典数据，执行失败的回调
                        //domain反转的域名 code 错误状态码，一般使用负数
                        let myError = NSError(domain: dataErrorDomain, code: -100000, userInfo: [ NSLocalizedDescriptionKey:"数据不合法"])
                        finished(dict: nil , error: myError)
                        return
                    }
                    //执行成功的回调
                    finished(dict: dict, error: nil)
                    
                }) { (_, error) -> Void in
                    //请求失败的回调
                    print(error)
                    //执行失败的回调
                    finished(dict: nil , error: error)
            }

        }
        
    }
    
    //上传图片的工具方法
    func uploadImage(urlString:String,parameters:[String:String]?,imageData:NSData, finished:(dict:[String:AnyObject]?,error:NSError?) -> ()){
    
    POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
        //将图片的二进制数据添加到formData中
formData.appendPartWithFileData(imageData, name: "pic", fileName: "xxoo", mimeType: "image/jpeg")
        
        }, progress: { (p) -> Void in
            print(p)
        }, success: { (_, result ) -> Void in
            print(result)
            //上传成功
            if let dict = result  as? [String:AnyObject]{
            
                finished(dict: dict , error: nil)
            }
        }) { (_, error) -> Void in
            print(error)
            //上传失败
            finished(dict: nil, error: error)
        }
    
    }
    
}
